﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSEViolationUserService
{
    private readonly SimpleDbContext _context;
    public QHSEViolationUserService(SimpleDbContext context)
    {
        _context = context;
    }
    public async Task<List<QHSEViolationUserModel>> GetAsync()
    {
        var keyWorkTypes = await _context.Set<QHSESysViolationUser>().ToListAsync();
        return MapperHelper.Map<List<QHSEViolationUserModel>>(keyWorkTypes);
    }
    public async Task<QHSEViolationUserModel> GetById(IdInputModel input)
    {
        var result = new QHSEViolationUserModel();

        if (string.IsNullOrEmpty(input.Id))
        {
            return result;

        }
        var user =await  _context.Set<QHSESysViolationUser>().AsNoTracking().Where(u=>u.ViolationUserId == input.Id).FirstOrDefaultAsync();
        if(user == null)
        {
            return result;
        }
        result =  MapperHelper.Map<QHSEViolationUserModel>(user);
        result.EmployeeTypeCode = await _context.Set<QHSESysDictionary>().AsNoTracking().Where(u => u.Item == result.EmployeeType).Select(u=>u.Code).FirstOrDefaultAsync();
        result.AgeCode = await _context.Set<QHSESysDictionary>().AsNoTracking().Where(u => u.Item == result.Age).Select(u => u.Code).FirstOrDefaultAsync();
        result.WorkYearsCode = await _context.Set<QHSESysDictionary>().AsNoTracking().Where(u => u.Item == result.WorkYears).Select(u => u.Code).FirstOrDefaultAsync();
        result.PositionCode = await _context.Set<QHSESysPosition>().AsNoTracking().Where(u => u.Position == result.Position).Select(u => u.PositionCode).FirstOrDefaultAsync();
        return result;
    }

    public async Task<int> AddAsync(QHSEViolationUserModel model)
    {
        var user = MapperHelper.Map<QHSESysViolationUser>(model);
        await _context.AddAsync(user);
        return await _context.SaveChangesAsync();
    }
}
